﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using ebay.Dal.CenterDB_BasTableAdapters;

namespace ebay.Dal
{
    public class PPDictionary
    {
        public void Add(List<ebay.Model.PPDictionary> list, string ebayUserId)
        {
            using (SqlConnection connection = new SqlConnection(SqlHelper.ceterDB_Conn))
            {
                connection.Open();
                T_PPDictionaryTableAdapter ppAdapter = new T_PPDictionaryTableAdapter();
                ppAdapter.Connection = connection;
                using (SqlTransaction trans =connection .BeginTransaction())
                {
                    ppAdapter.Transaction = trans;

                    if (ppAdapter.ScalarQueryByEbayUserId(ebayUserId).Value > 0)
                    {
                        foreach (ebay.Model.PPDictionary m in list)
                        {
                            ppAdapter.Update(m.CValue, m.EbayUserId, m.CKey);
                        }
                    }
                    else
                    {
                        foreach (ebay.Model.PPDictionary m in list)
                        {
                            ppAdapter.Insert(m.EbayUserId, m.CKey, m.CValue);
                        }
                    }
                    try
                    {
                        trans.Commit();
                    }
                    catch
                    {
                        trans.Rollback();
                    }
                }
            }
        }
    }
}
